package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.service.admin.MarketConfigService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @MethodName AdminConfigManagerController
 * @Description  配置管理模块
 * @Author M
 * @Date 16:00 2023/5/9
 **/
@RequiresAuthentication
@RestController
@RequestMapping("admin/config")
public class AdminConfigManagerController {

    @Autowired
    private MarketConfigService configService;

    /*
    "market_mall_longitude": "阿",
	"market_mall_latitude": "acacia",
	"market_mall_address": "gu",
	"market_mall_qq": "1214",
	"market_mall_phone": "个人",
	"market_mall_name": "123"
     */
    @GetMapping("mall")
    public Object mallGet( ) throws IOException {
        Map<String, Object> result = new HashMap<>();
        // 参数
        String longitude = configService.getKeyValueByKeyName("market_mall_longitude");
        String latitude = configService.getKeyValueByKeyName("market_mall_latitude");
        String address = configService.getKeyValueByKeyName("market_mall_address");
        String qq = configService.getKeyValueByKeyName("market_mall_qq");
        String phone = configService.getKeyValueByKeyName("market_mall_phone");
        String name = configService.getKeyValueByKeyName("market_mall_name");

        result.put("market_mall_longitude", longitude);
        result.put("market_mall_latitude", latitude);
        result.put("market_mall_address", address);
        result.put("market_mall_qq", qq);
        result.put("market_mall_phone", phone);
        result.put("market_mall_name", name);

        return ResponseUtil.ok(result);
    }

    /*
        "market_wx_index_new": "5",
	    "market_wx_index_topic": "5",
	    "market_wx_share": "true",
	    "market_wx_index_brand": "2",
	    "market_wx_catlog_goods": "4",
	    "market_wx_catlog_list": "4",
	    "market_wx_index_hot": "6"
     */
    @GetMapping("wx")
    public Object wxGet( ) throws IOException {
        Map<String, Object> result = new HashMap<>();
        // 参数
        String indexNew = configService.getKeyValueByKeyName("market_wx_index_new");
        String indexTopic = configService.getKeyValueByKeyName("market_wx_index_topic");
        String share = configService.getKeyValueByKeyName("market_wx_share");
        String indexBrand = configService.getKeyValueByKeyName("market_wx_index_brand");
        String catlogGoods = configService.getKeyValueByKeyName("market_wx_catlog_goods");
        String catlogList = configService.getKeyValueByKeyName("market_wx_catlog_list");
        String indexHot = configService.getKeyValueByKeyName("market_wx_index_hot");

        result.put("market_wx_index_new", indexNew);
        result.put("market_wx_index_topic", indexTopic);
        result.put("market_wx_share", share);
        result.put("market_wx_index_brand", indexBrand);
        result.put("market_wx_catlog_goods", catlogGoods);
        result.put("market_wx_catlog_list", catlogList);
        result.put("market_wx_index_hot", indexHot);


        return ResponseUtil.ok(result);
    }

    /*
        {
	"errno": 0,
	"data": {
		"market_order_unconfirm": "8",
		"market_order_unpaid": "31",
		"market_order_comment": "8"
	},
	"errmsg": "成功"
    }
     */
    @GetMapping("order")
    public Object orderGet() throws IOException {
        Map<String, Object> result = new HashMap<>();
        // 参数
        String unconfirm = configService.getKeyValueByKeyName("market_order_unconfirm");
        String unpaid = configService.getKeyValueByKeyName("market_order_unpaid");
        String comment = configService.getKeyValueByKeyName("market_order_comment");

        result.put("market_order_unconfirm", unconfirm);
        result.put("market_order_unpaid", unpaid);
        result.put("market_order_comment", comment);
        return ResponseUtil.ok(result);
    }

    /*
    {
	"errno": 0,
	"data": {
		"market_express_freight_min": "wdwangdao",
		"market_express_freight_value": "javajava"
	},
	"errmsg": "成功"
    }
     */
    // 返回请求参数，查找market_system表中的数据
    @GetMapping("express")
    public Object expressGet() throws IOException {
        Map<String, Object> result = new HashMap<>();
        // 参数
        String freightMin = configService.getKeyValueByKeyName("market_express_freight_min");
        String freightValue = configService.getKeyValueByKeyName("market_express_freight_value");

        result.put("market_express_freight_min", freightMin);
        result.put("market_express_freight_value", freightValue);

        return ResponseUtil.ok(result);
    }

    /*
    "market_mall_longitude": "阿",
	"market_mall_latitude": "acacia",
	"market_mall_address": "gu",
	"market_mall_qq": "1214",
	"market_mall_phone": "个人",
	"market_mall_name": "123"
     */
    @PostMapping("mall")
    public Object mallPost(@RequestBody Map<String,String> map) throws IOException {
        // 解析请求参数

        String longitude = map.get("market_mall_longitude");
        String latitude = map.get("market_mall_latitude");
        String address = map.get("market_mall_address");
        String qq = map.get("market_mall_address");
        String phone = map.get("market_mall_phone");
        String name = map.get("market_mall_name");

        // 使用数据对表中数据进行修改
        int row = configService.setMallConfig(longitude, latitude, address, qq, phone, name);

        if (row != 1) {
            // 删除失败
            return ResponseUtil.badArgument();
        }
        // 删除成功
        return ResponseUtil.ok();
    }

    @PostMapping("wx")
    public Object wxPost(@RequestBody Map<String,String> map) throws IOException {
        // 解析请求参数

        String indexNew = map.get( "market_wx_index_new");
        String indexTopic = map.get("market_wx_index_topic");
        String share = map.get("market_wx_share");
        String indexBrand = map.get("market_wx_index_brand");
        String catlogGoods = map.get("market_wx_catlog_goods");
        String catlogList = map.get("market_wx_catlog_list");
        String indexHot = map.get("market_wx_index_hot");

        // 使用数据对表中数据进行修改
        int row = configService.setWxConfig(indexNew, indexTopic, share, indexBrand, catlogGoods, catlogList, indexHot);


        if (row != 1) {
            // 删除失败
            return ResponseUtil.badArgument();
        }
        // 删除成功
        return ResponseUtil.ok();
    }

    /*
        {
	        "market_order_unconfirm": "8",
	        "market_order_unpaid": "31",
	        "market_order_comment": "8"
        }
     */
    @PostMapping("order")
    public Object orderPost(@RequestBody Map<String,String> map) throws IOException {
        // 解析请求参数
        String unconfirm = map.get("market_order_unconfirm");
        String unpaid = map.get("market_order_unpaid");
        String comment = map.get("market_order_comment");
        // 使用数据对表中数据进行修改
        int row = configService.setOrderConfig(unconfirm, unpaid, comment);
        if (row != 1) {
            // 删除失败
            return ResponseUtil.badArgument();
        }
        // 删除成功
        return ResponseUtil.ok();
    }

    /*
        请求参数：
        {"market_express_freight_min":"wdwangdao","market_express_freight_value":"javajava"}
     */
    @PostMapping("express")
    public Object expressPost(@RequestBody Map<String,String> map) throws IOException {
        // 解析请求参数
        String freightMin = map.get("market_express_freight_min");
        String freightValue = map.get("market_express_freight_value");
        // 使用数据对表中数据进行修改
        int row = configService.setExpressConfig(freightMin, freightValue);
        if (row != 1) {
            // 删除失败
            return ResponseUtil.badArgument();
        }
        // 删除成功
        return ResponseUtil.ok();
    }
}
